#include "common.h"
#include <fcntl.h>

int main()
{
	pid_t pid;
	int fd;

	pid = fork();

	if(pid < 0)
		err_sys("fork error\n");
	else if(pid == 0)
	{
		char data[1024] = {0};

		printf("before open by readonly\n");
		sleep(5);
		fd = open("chatA", O_RDONLY);
		if(fd < 0)
			err_sys("open fail\n");

		printf("after open by readonly\n");

		printf("before read by readonly\n");
/*
		read(fd, data, 1024);
		printf("Parent : %s\n", data);
*/
		while(1)
		{
			bzero(data, 1024);
			read(fd, data, 1024);
			printf("Parent : %s\n", data);
			sleep(1);
		}
	}
	else
	{
		char data[] = "hello child\n";

		printf("Writeonly_before\n");

		fd = open("chatA", O_WRONLY);
		if(fd < 0)
			err_sys("open fail\n");

		printf("Writeonly_after\n");

		printf("before_write\n");
/*
		write(fd, data, strlen(data));
		printf("after_write\n");
*/
		while(1)
		{
			write(fd, data, strlen(data));
			printf("after_write\n");
			sleep(1);
		}
	}

	return 0;
}
